package cn.tedu.controller;

import cn.tedu.pojo.User;
import cn.tedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @RequestMapping("/toregister")
    public String toregister(User user,HttpServletRequest request){
        boolean flag = userService.register(user);
        if(flag){
            return "redirect:/login.do";
        }else {
            request.setAttribute("msg","用户名已存在");
            return "register";
        }
    }
    @RequestMapping("/login")
    public String login(User user){
        return "login";
    }
    @RequestMapping("/tologin")
    public String tologin(User user, HttpServletRequest request){
        boolean flag = userService.login(user);
        if(flag){
            return "redirect:/productListPageHelper.do";
        }else {
            request.setAttribute("msg","用户名或密码错误");
            return "login";
        }
    }
}
